﻿using AdminWinForm.Services;

namespace AdminWinForm;

public static class AppConfig
{
    public static string AppId => "KIMS";
    public static string AppName => "Known信息管理系统";

    public static void AddApplication(this IServiceCollection services)
    {
        Console.WriteLine(AppName);
#if DEBUG
        Config.IsDevelopment = true;
        Config.IsDebug = true;
#endif
        //Stopwatcher.Enabled = true;
        services.AddAppWin();
        services.AddAppWinCore();
    }

    private static void AddAppWin(this IServiceCollection services)
    {
        var assembly = typeof(AppConfig).Assembly;
        services.AddKnown(info =>
        {
            info.Id = AppId;
            info.Name = AppName;
            info.Assembly = assembly;
            //JS路径，通过JS.InvokeAppVoidAsync调用JS方法
            //info.JsPath = "./script.js";
        });
        services.AddKnownAdmin();

        //UIConfig.AutoTablePage = (b, m) => b.Component<CustomTablePage>().Set(c => c.Model, m).Build();
        UIConfig.Errors["403"] = new ErrorConfigInfo { Description = "你没有此页面的访问权限。" };
    }

    private static void AddAppWinCore(this IServiceCollection services)
    {
        services.AddKnownAdminCore(option =>
        {
            //option.Code = new CodeConfigInfo
            //{
            //    EntityPath = @"D:\Sample",
            //    PagePath = @"D:\Sample.Client",
            //    ServicePath = @"D:\Sample.Web"
            //};
            //option.AddModules(ModuleHelper.AddAppModules);
        });
        services.AddKnownCells();
        services.AddKnownWin(option =>
        {
            option.App.WebRoot = Application.StartupPath;
            option.App.ContentRoot = Application.StartupPath;
            option.Database = db =>
            {
                //db.AddAccess<System.Data.OleDb.OleDbFactory>("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=KnownAdmin;Jet OLEDB:Database Password={password}");
                db.AddSQLite<Microsoft.Data.Sqlite.SqliteFactory>("Data Source=..\\KnownAdmin.db");
                //db.AddSqlServer<Microsoft.Data.SqlClient.SqlClientFactory>("Server=(localdb)\\MSSQLLocalDB;Database=KnownAdmin;Trusted_Connection=True");
                //db.AddMySql<MySqlConnector.MySqlConnectorFactory>("Data Source=localhost;port=3306;Initial Catalog=KnownAdmin;user id={userId};password={password};Charset=utf8;SslMode=none;AllowZeroDateTime=True;");
                //db.AddPgSql<Npgsql.NpgsqlFactory>("Data Source=localhost;Initial Catalog=KnownAdmin;User Id={userId};Password={password};");
                //db.AddDM<Dm.DmClientFactory>("Server=localhost;Schema=KnownAdmin;DATABASE=KnownAdmin;uid=xxx;pwd=xxx;");
                //db.SqlMonitor = c => Console.WriteLine($"{DateTime.Now:HH:mm:ss} {c}");
            };

            //option.ProductId = "Test";
            //option.CheckSystem = info => Result.Error("无效密钥，请重新授权！");
        });

        // 注入服务
        services.AddScoped<IHomeService, HomeService>();
    }
}